package tpenchere.ui.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;

import javax.swing.table.AbstractTableModel;

import tpenchere.model.Produit;

public class ProductTableModel extends AbstractTableModel {
	private String[] columnNames = {
			"Id",
			"Product Label", 
			"Description", 
			"Category", 
			"Owner", 
			"Starting Bid", 
			"Current Bid",
			"Due date"
			};
	private ArrayList<ArrayList<Object>> data = new ArrayList<ArrayList<Object>>();
	//{
			//{1, "Bob's printer", "Awesome", "Miscellaneous", "Bob", 100.50, 300.50, new Date()},
			//{2, "Judith's printer", "Amazing", "Miscellaneous", "Judith", 120.50, 590.09, new Date()},
			//{3, "Bobby's", "Da", "Cat1", "Judith", 120.50, 590.09, new Date()},
			//{4, "John", "Amazing", "Cat2", "Batman", 120.50, 590.09, new Date()},
			//{5, "Jean", "De", "Miscellaneous", "Judith", 120.50, 590.09, new Date()}
	//};
	
	
	public String getCategory(int row) {
		int index = -1;
		for(int i=0; i < columnNames.length; i++) {
			if(columnNames[i] == "Category"){
				index = i;
				break;
			}
		}
		if(index != -1) {
			return data.get(row).get(index).toString();			
		} else {
			return "";
		}
	}
	
	public void insertRowProduct(Produit product) {
		ArrayList<Object> line = new ArrayList<Object>();
		
		line.add(product.idProduit);
		line.add(product.libelle);
		line.add(product.descriptif);
		line.add(product.categorie.nom);
		line.add(product.pseudoVendeur);
		line.add(product.prixDepart);
		line.add(product.prixImmediat);
		line.add(new Date(product.echeance));
		
		data.add(line);
		int row = data.indexOf(line);
		fireTableRowsInserted(row, row);
	}
	
	public int getColumnCount() {
		return columnNames.length;
	}

	public int getRowCount() {
		return data.size();
	}

	public String getColumnName(int col) {
		return columnNames[col];
	}

	public Object getValueAt(int row, int col) {
		try {
			return data.get(row).get(col);			
		} catch(IndexOutOfBoundsException iobe) {
			return null;
		}
	}

	public Class getColumnClass(int c) {
		if(getValueAt(0, c) != null) {
			return getValueAt(0, c).getClass();			
		}
		return Object.class;
	}

	/*
	 * Don't need to implement this method unless your table's editable.
	 */
	public boolean isCellEditable(int row, int col) {
		// Note that the data/cell address is constant,
		// no matter where the cell appears onscreen.
		return false;
	}

	/*
	 * Don't need to implement this method unless your table's data can change.
	 */
	public void setValueAt(Object value, int row, int col) {
		data.get(row).set(col, value);
		fireTableCellUpdated(row, col);
	}
	
	public void clearData() {
		data.clear();
		fireTableDataChanged();
	}
}
